﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SilverlightSdms.AdminService;
using System.Collections.ObjectModel;
using SilverlightSdms.FacilitiesService;
using System.Globalization;

namespace SilverlightSdms.Views
{
    public partial class TeamLeader : Page
    {
        int m_CurrentTeamLeaderID = 4; //for testing - Shiran

        public User m_CurrentTeamLeader { get; set; }

        public User m_CurrentTeamMemberSelectedForInfo { get; set; }
        public User m_CurrentTeamMemberSelectedForEditVisits { get; set; }

        bool m_IsLoadedMyInfo = false;
        bool m_IsLoadedTeamInfo = false;
        bool m_IsLoadedFacilities = false;

        public ObservableCollection<User> TeamMembers { get; set; }

        public ObservableCollection<FacilitieInfo> TeamSchools { get; set; }
        
        public TeamLeader()
        {
            InitializeComponent();

            TeamSchools = new ObservableCollection<FacilitieInfo>();

            TeamMembers = new ObservableCollection<User>();

            // getting the id of the current user
            m_CurrentTeamLeaderID = ((App)Application.Current).UserId;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        // Executes when the user navigates to another page.
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            m_CurrentTeamLeader = null;

            m_IsLoadedMyInfo = false; 
            m_IsLoadedTeamInfo = false;
            m_IsLoadedFacilities = false;

            base.OnNavigatedFrom(e);
        }


        private void tabItemMyTeam_Loaded(object sender, RoutedEventArgs e)
        {
            if (!m_IsLoadedMyInfo)
            {
                m_IsLoadedMyInfo = true;

                busyIndicator.IsBusy = true;

                var client = new UsersServiceClient();
                client.GetUserCompleted += (s, ea) =>
                {
                    m_CurrentTeamLeader = ea.Result;

                    // fill members lists

                    this.listBoxTeamMembers.ItemsSource = TeamMembers;
                    this.listBoxTeamMembers.DisplayMemberPath = "Name";

                    this.listBoxTeamMembers2.ItemsSource = TeamMembers;
                    this.listBoxTeamMembers2.DisplayMemberPath = "Name";

                    client.GetTeamUsersCompleted += (s2, ea2) =>
                    {
                        var selectedItem = this.listBoxTeamMembers.SelectedItem;
                        var selectedItem2 = this.listBoxTeamMembers2.SelectedItem;
                        TeamMembers.Clear();

                        if (ea2.Result.Count > 0)
                        {
                            foreach (var item in ea2.Result)
                            {
                                TeamMembers.Add(item);
                            }

                            if (selectedItem != null)
                            {
                                this.listBoxTeamMembers.SelectedItem = selectedItem;
                            }
                            else
                            {
                                this.listBoxTeamMembers.SelectedIndex = 0;
                            }

                            if (selectedItem2 != null)
                            {
                                this.listBoxTeamMembers2.SelectedItem = selectedItem2;
                            }
                            else
                            {
                                this.listBoxTeamMembers2.SelectedIndex = 0;
                            }
                        }
                        busyIndicator.IsBusy = false;
                    };

                    client.GetTeamUsersAsync(m_CurrentTeamLeaderID);
                    busyIndicator.IsBusy = false;
                };

                client.GetUserAsync(m_CurrentTeamLeaderID);
            }
        }

        private void tabItemTeamVisits_Loaded(object sender, RoutedEventArgs e)
        {
            if (!m_IsLoadedTeamInfo)
            {
                m_IsLoadedTeamInfo = true;

                busyIndicator.IsBusy = true;

                buildMonthButtonsForTeamVisits();

                busyIndicator.IsBusy = false;
            }
        }

        private void listBoxTeamMembers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                m_CurrentTeamMemberSelectedForInfo = (User)e.AddedItems[0];
                LayoutRootTeamInfo.DataContext = m_CurrentTeamMemberSelectedForInfo;
            }
        }

        private void tabItemSchools_Loaded(object sender, RoutedEventArgs e)
        {
            if (!m_IsLoadedFacilities)
            {
                m_IsLoadedFacilities = true;

                busyIndicator.IsBusy = true;

                this.listBoxSchools.ItemsSource = TeamSchools;
                this.listBoxSchools.DisplayMemberPath = "Name";

                var client = new FacilitiesService.FacilitiesServiceClient();

                client.GetFacilitiesForTeamMemberCompleted += (s, ea) =>
                {
                    var selectedItem = this.listBoxSchools.SelectedItem;

                    TeamSchools.Clear();

                    if (ea.Result.Count > 0)
                    {
                        foreach (var item in ea.Result)
                        {
                            TeamSchools.Add(item);
                        }

                        if (selectedItem != null)
                        {
                            this.listBoxSchools.SelectedItem = selectedItem;
                        }
                        else
                        {
                            this.listBoxSchools.SelectedIndex = 0;
                        }
                    }
                    busyIndicator.IsBusy = false;
                };

                client.GetFacilitiesForTeamMemberAsync(m_CurrentTeamLeaderID);
            }
        }

        private void listBoxSchools_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                busyIndicator.IsBusy = true;

                var client = new VisitsService.VisitsServiceClient();

                client.GetTeamVisitsForSpecificsSchoolCompleted += (s, ea) =>
                {
                    dataGridSchoolsVisits.ItemsSource = ea.Result;

                    busyIndicator.IsBusy = false;
                };

                client.GetTeamVisitsForSpecificsSchoolAsync(m_CurrentTeamLeaderID, ((FacilitieInfo)e.AddedItems[0]).FacilitieId);
            }
        }

        private void listBoxTeamMembers2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                m_CurrentTeamMemberSelectedForEditVisits = (User)e.AddedItems[0];
                EditVisitsScheduler.GetCalendarForUser(m_CurrentTeamMemberSelectedForEditVisits.Id);
            }
        }

        private void buildMonthButtonsForTeamVisits()
        {
            Button[] btnsMonths = new Button[12];

            string[] months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
            
            userControlTeamVisits.Children.Clear();
            for (int i = months.Length - 2; i >= 0; i--)
            {
                btnsMonths[i] = new Button();
                btnsMonths[i].HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                btnsMonths[i].VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                //btnsMonths[i].Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xE5, 0xE5, 0xE5));  // alpha, red, green, blue
                btnsMonths[i].Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xA8, 0xFF));
                btnsMonths[i].DataContext = new DateTime(2012, i + 1, 1);
                btnsMonths[i].Content = months[i];
                btnsMonths[i].Margin = new Thickness(0, 0, 0, 4);
                btnsMonths[i].Width = 80;
                btnsMonths[i].Click += new RoutedEventHandler(GetVisitsButton_Click);
                userControlTeamVisits.Children.Add(btnsMonths[i]);
            }

        }

        void GetVisitsButton_Click(object sender, RoutedEventArgs e)
        {
            int month;
            if (sender == buttonAllVisits)
            {
                month = 13;
            }
            else
            {
                month = ((DateTime)((sender as Button).DataContext)).Month;
            }

            // fill team's visits dataGrid
            var client = new VisitsService.VisitsServiceClient();

            busyIndicator.IsBusy = true;

            client.GetVisitsForTeamCompleted += (s, ea) =>
            {
                teamsVisitsDataGrid.AutoGenerateColumns = true;
                teamsVisitsDataGrid.ItemsSource = ea.Result;

                busyIndicator.IsBusy = false;
            };

            ObservableCollection<int> TeamMembersIDs = new ObservableCollection<int>();
            foreach (User user in TeamMembers)
            {
                TeamMembersIDs.Add(user.Id);
            }

            client.GetVisitsForTeamAsync(TeamMembersIDs, month);
        }
    }
}